<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>组织架构</h2>
      <el-card class="tree-card">
        <treeTools :data="company" :is-root="true" />
        <hr>
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <treeTools
            slot-scope="{ data }"
            :data="data"
            @del-depts="delDepartment"
            @showdialog="show(data)"
            @editDepartment="editDepartment"
          />
        </el-tree>
      </el-card>
    </div>
    <addDepartmentdDialog ref="addDepartmentdDialog" :dialog="dialogFormVisible" :data="departments" @closeDialog="closeDialog" @cancel="cancel" />
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import treeTools from './components/tree-tools'
import addDepartmentdDialog from './components/addDepartmentdDialog'
export default {
  components: {
    treeTools,
    addDepartmentdDialog
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},
      dialogFormVisible: false,
      departments: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    editDepartment(data) {
      this.dialogFormVisible = true
      this.departments = data
      this.$refs.addDepartmentdDialog.getDepartmentDetail(data.id)
    },
    show(data) {
      // console.log(2)
      console.log(data)
      this.dialogFormVisible = true
      this.departments = data
    },
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)

      this.company = {
        name: res.companyName,
        manager: '负责人'
      }
      this.departs = transListToTreeData(res.depts, '')
    },
    delDepartment() {
      this.getDepartments()
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.getDepartments()
    },
    cancel() {
      this.dialogFormVisible = false
    }

  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
